package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.SalesDetail;
import com.ruoyi.system.mapper.SalesOrderMapper;
import com.ruoyi.system.domain.SalesOrder;
import com.ruoyi.system.service.ISalesOrderService;

/**
 * 销售订单Service业务层处理
 * 
 * @author 廖瀚
 * @date 2025-05-20
 */
@Service
public class SalesOrderServiceImpl implements ISalesOrderService 
{
    @Autowired
    private SalesOrderMapper salesOrderMapper;

    /**
     * 查询销售订单
     * 
     * @param id 销售订单主键
     * @return 销售订单
     */
    @Override
    public SalesOrder selectSalesOrderById(Long id)
    {
        return salesOrderMapper.selectSalesOrderById(id);
    }

    /**
     * 查询销售订单列表
     * 
     * @param salesOrder 销售订单
     * @return 销售订单
     */
    @Override
    public List<SalesOrder> selectSalesOrderList(SalesOrder salesOrder)
    {
        return salesOrderMapper.selectSalesOrderList(salesOrder);
    }

    /**
     * 新增销售订单
     * 
     * @param salesOrder 销售订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSalesOrder(SalesOrder salesOrder)
    {
        int rows = salesOrderMapper.insertSalesOrder(salesOrder);
        insertSalesDetail(salesOrder);
        return rows;
    }

    /**
     * 修改销售订单
     * 
     * @param salesOrder 销售订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSalesOrder(SalesOrder salesOrder)
    {
        salesOrderMapper.deleteSalesDetailByOrderId(salesOrder.getId());
        insertSalesDetail(salesOrder);
        return salesOrderMapper.updateSalesOrder(salesOrder);
    }

    /**
     * 批量删除销售订单
     * 
     * @param ids 需要删除的销售订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSalesOrderByIds(Long[] ids)
    {
        salesOrderMapper.deleteSalesDetailByOrderIds(ids);
        return salesOrderMapper.deleteSalesOrderByIds(ids);
    }

    /**
     * 删除销售订单信息
     * 
     * @param id 销售订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSalesOrderById(Long id)
    {
        salesOrderMapper.deleteSalesDetailByOrderId(id);
        return salesOrderMapper.deleteSalesOrderById(id);
    }

    /**
     * 新增销售订单明细信息
     * 
     * @param salesOrder 销售订单对象
     */
    public void insertSalesDetail(SalesOrder salesOrder)
    {
        List<SalesDetail> salesDetailList = salesOrder.getSalesDetailList();
        Long id = salesOrder.getId();
        if (StringUtils.isNotNull(salesDetailList))
        {
            List<SalesDetail> list = new ArrayList<SalesDetail>();
            for (SalesDetail salesDetail : salesDetailList)
            {
                salesDetail.setOrderId(id);
                list.add(salesDetail);
            }
            if (list.size() > 0)
            {
                salesOrderMapper.batchSalesDetail(list);
            }
        }
    }
}
